<template>
	<view class="content" style="overflow: hidden;">
		<view class="text-area">
			<view class="title">弘兆软件</view>
			<view class="title title_last">智能化管理系统</view>
			<view class="userInfo">
				<view>
					<text>账 号:</text>
					<input type="text" v-model="id">
				</view>
				<view>
					<text>密 码:</text>
					<input type="password" v-model="psw">
				</view>
				<view>
					<checkbox-group @change="checkboxChange" style="display: flex;">
						<label class="uni-list-cell uni-list-cell-pd">
							<view>
								<checkbox :checked="istrue" />
							</view>
						</label>
						记住用户
					</checkbox-group>
				</view>
				<view  style="position: fixed;bottom: 200upx;width: 100%;text-align: center;box-sizing: border-box;
				display: flex;align-items: center;justify-content: center;" @tap="saomiao">扫码配置服务地址</view>
			</view>
			<view class="info">
				<view>当前版本:{{v}}</view>
				<!-- <view>禾冰袜业</view> -->
			</view>
			<view class="btns">
				<button @tap="setting">设置</button>
				<button @tap="login">登陆</button>
			</view>
		</view>
	</view>
</template>

<script>
	import axios from '@/static/axios.js';
	// import Voice from '@/static/voice.js'; //语音文件
	const TTSSpeech = uni.requireNativePlugin("MT-TTS-Speech");
	export default {
		data() {
			return {
				title: '',
				v: "1.0.0",
				id: '',
				psw: '',
				istrue: true,
				url: '',
				uid:'',
			}
		},
		onShow() {
			this.url = uni.getStorageSync('url') == '' ? this.websiteUrl : uni.getStorageSync('url')
		},
		onLoad() {
			console.log('d')
			// uni.getSystemInfo({
			// 	success(r) {
			// 		console.log(r,'rr')
			// 	}
			// })
			TTSSpeech.init((status) => {
				if (status === 0) {
					// console.log('引擎初始化成功')
				}
			}, 'com.iflytek.speechcloud')
			this.id = uni.getStorageSync('id') ? uni.getStorageSync('id') : ''
			this.uid = uni.getStorageSync('uid') ? uni.getStorageSync('uid') : ''
			this.psw = uni.getStorageSync('psw') ? uni.getStorageSync('psw') : ''
			this.url = uni.getStorageSync('url') == '' ? this.websiteUrl : uni.getStorageSync('url')
			this.v = uni.getStorageSync('bbh') == '' ? this.Version : uni.getStorageSync('bbh')
		},
		methods: {
			saomiao() {
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode', 'barCode'], //这里有个坑，不写的话扫描速度有点慢
					success: (res) => {
						if (res.result) {
							console.log(res)
							uni.setStorageSync('url',res.result)
							uni.showToast({
								title:'服务地址更新成功!',
								icon:'none',
								duration:3000,
							})
						}
					}
				})
			},
			checkboxChange(e) {
				this.istrue = !this.istrue
			},
			login() {
				
				console.log(this.url,'this.url')
				if (this.id == '') {
					uni.showToast({
						title: '请输入账号',
						icon: 'none',
						duration: 3000
					})
					return
				} else if (this.psw == '') {
					uni.showToast({
						title: '密码不能为空',
						icon: 'none',
						duration: 3000
					})
					return
				} 
				console.log(`http://${this.url}/bService.asmx/LoginGetMenu`);
				uni.request({
					url: `http://${this.url}/bService.asmx/LoginGetMenu`,
					method: 'GET',
					data: {
						ls_uid: this.id,
						ls_pwd: this.psw
					},
					success: (r) => {
						console.log(r.data, 'rrr')
						if (r.data.state == 'success' || r.data.state == 'true') {
							if (!!this.istrue) {
								uni.setStorageSync('id', r.data.us_id)
								uni.setStorageSync('uid', r.data.us_id)//之前取值是uid,没有uid这个字段,重新加上去的
								uni.setStorageSync('psw', this.psw)
								uni.setStorageSync('uname', !!r.data.us_name?r.data.us_name:r.data.uname)
								uni.setStorageSync('ck', r.data.ck)
								uni.setStorageSync('pdsl', r.data.pdsl)
								uni.setStorageSync('isyp', r.data.isYP)
								uni.setStorageSync('wwmr', r.data.scjww == undefined?'否':r.data.scjww )  //委外出库是否默认
								uni.setStorageSync('CustomName', !r.data.CustomName?'null':r.data.CustomName )  //项目名称
							} else {
								uni.setStorageSync('id', '')
								uni.setStorageSync('uid', '')//之前取值是uid,没有uid这个字段,重新加上去的
								uni.setStorageSync('psw', '')
								uni.setStorageSync('uname', '')
								uni.setStorageSync('ck', '')
								uni.setStorageSync('isyp', '')
								uni.setStorageSync('wwmr', '')
								uni.setStorageSync('CustomName', '')
							}
							uni.reLaunch({
								url: '../index/index'
							})
						} else if (r.data[0] != undefined && r.data[0].state == 'success') {

							if (!!this.istrue) {
								uni.setStorageSync('id', r.data[0].uid)
								uni.setStorageSync('uid', r.data[0].uid)//之前取值是uid,没有uid这个字段,重新加上去的
								uni.setStorageSync('psw', this.psw)
								uni.setStorageSync('uname', !!r.data[0].us_name?r.data[0].us_name:r.data[0].uname)
								uni.setStorageSync('ck', r.data[0].ck)
							} else {
								uni.setStorageSync('id', '')
								uni.setStorageSync('uid', '')
								uni.setStorageSync('psw', '')
								uni.setStorageSync('uname', '')
								uni.setStorageSync('ck', '')
							}
							uni.reLaunch({
								url: '../index/index'
							})
						} else {
							// Voice(r.data.msg, true)
							TTSSpeech.speak({
								text: r.data.msg
							})
							uni.showToast({
								title: r.data.msg,
								icon: 'none',
								duration: 3000
							})
						}
					},
					complete: (res) => {
						console.log(res)
						if (res.statusCode != 200) {
							uni.showToast({
								title: '网络请求失败!',
								icon: 'none',
								duration: 3000
							})
						}
					}
				})

			},
			setting() {
				uni.navigateTo({
					url: '../setting/setting'
				})
			}
		}
	}
</script>

<style>
	.title {
		color: #f0301f;
		font-size: 120upx;
		text-align: center;
		font-weight: 500;
		margin-top: 30upx;
	}

	.title_last {
		font-size: 90upx;
		color: #000000;
		margin: 20upx 0;
	}

	.userInfo {
		margin: 30upx auto;
	}

	.userInfo>view {
		display: flex;
		align-items: center;
		height: 100upx;
		padding: 0 100upx;
	}

	.userInfo>view text {
		display: inline-block;
		margin-right: 10upx;
		width: 100upx;
	}

	input {
		border: 1px solid #ccc;
		height: 70upx;
		border-radius: 10upx;
	}

	.btns {
		display: flex;
		align-items: center;
		position: fixed;
		bottom: 10upx;
		padding: 0 10upx;
		width: 100%;
		justify-content: space-between;
		height: 100upx;
		box-sizing: border-box;
	}

	.btns button {
		width: 45%;
		line-height: 90upx;
		background-color: #c3d9d9;
	}

	.info {
		display: flex;
		padding: 0 30upx;
		justify-content: space-between;
		position: fixed;
		bottom: 130upx;
		width: 100%;
		box-sizing: border-box;
	}

	uni-checkbox-group {
		display: flex;
	}
</style>
